<?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			site_specific/search.php
#	FUNCTION:		Handles the searching
#	AUTHOR:			Cameron Morrow
#	CREATED:		05/10/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Get search term
$SEARCH_TERMS = (@$_GET["search"]) ? $_GET["search"] : "";

# Remove bad chars
$SEARCH_TERMS = removeInvalidChars($SEARCH_TERMS, array_merge($VALID_CHARS_LETTERS, $VALID_CHARS_LETTERS_UPPER, array(" ")));

# Divide
$SEARCH_TERMS = explode(" ", $SEARCH_TERMS);

if (implode("", $SEARCH_TERMS) != "") {

	# Get members
	$members_sql = "SELECT u_id, u_desc, u_login_name, u_region, u_category FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_group  = " . getBusinessGroupID() . " AND (";
	$field_array = array("u_login_name", "u_desc");
	foreach ($field_array as $field_counter => $field) {
		foreach ($SEARCH_TERMS as $term_counter => $term) {
			$members_sql .= $field . " LIKE '%" . $term . "%'";
			if ($term_counter < count($SEARCH_TERMS) - 1) {
				$members_sql .= " OR ";
			}
		}
		if ($field_counter < count($field_array) - 1) {
			$members_sql .= " OR ";
		}
	}
	$members_sql .= ") AND u_category != '' AND u_region != '' ORDER BY u_login_name ASC";

	# Data
	$members_record = getRecord($members_sql);

	# Get pages
	$pages_sql = "SELECT p_id, p_name, p_parent, p_accesstype, p_access, p_section, p_content FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_accessdatestart <= '" . getFullDate() . "' AND p_accessdateend >= '" . getFullDate() . "' AND p_status != 0";

	$pages_record = getRecord($pages_sql);

	# For each one, get access level
	foreach ($pages_record as $pc => $page) {

		# Remove tags from content
		$pages_record[$pc]["p_content"] = removeTags($page["p_content"]);

		# Check if contains relevant results
		$found_results = true;

		foreach ($SEARCH_TERMS as $search_term) {
			if (strpos(strtolower($pages_record[$pc]["p_content"]), strtolower($search_term)) === false) {
				$found_results = false;
			}
		}

		# Store results
		$pages_record[$pc]["found"] = $found_results;

		# If inheriting
		if ($page["p_accesstype"] == "0") {

			# Go up the tree to find defined type
			$parent_id = $page["p_parent"];

			# Top level access
			$parent_access = $page["p_access"];

			# While haven't found the parent
			while ($parent_id != "") {

				foreach ($pages_record as $pc2 => $parent_page) {
					if ($parent_page["p_id"] == $parent_id) {
						$parent_id = $parent_page["p_parent"];
						$parent_access = $parent_page["p_access"];
					}
				}
			}

			# Define access
			$pages_record[$pc]["p_access"] = $parent_access;
		}

		# Check for authorization
		$current_group = $USER -> getGroup();
		$page_access = $pages_record[$pc]["p_access"];

		# Authorized
		$pages_record[$pc]["allowed"] = ($page_access == $current_group || strpos($page_access, "," . $current_group . ",") !== false || substr($page_access, 0, strpos($page_access, ",")) == $current_group || substr($page_access, strrpos($page_access, ",") + 1) == $current_group);
	}

	# Remove ones that a) don't have results or b) aren't authorized
	for ($i = 0; $i < count($pages_record); $i++) {
		if (!$pages_record[$i]["allowed"] || !$pages_record[$i]["found"]) {
			array_splice($pages_record, $i, 1);
			$i--;
		}
	}


	# Header
	addContent("<h2>Search results for: <em>" . implode(" ", $SEARCH_TERMS) . "</em> (" . (count($pages_record) + count($members_record)) . " results)</h2>");

	# Members
	addContent("<a name=\"members\"></a><h3>Members: (go to <a href=\"#pages\">pages</a>)</h3>");
	addContent("<ul class=\"sponsorlist\">");
	foreach ($members_record as $mc => $member) {

		$category = explode(",", $member["u_category"]);
		for ($i = 0; $i < count($category); $i++) {
			if ($category[$i] == "0") {
				array_splice($category, $i, 1);
				$i--;
			}
		}

		$region = explode(",", $member["u_region"]);
		for ($i = 0; $i < count($region); $i++) {
			if ($region[$i] == "0") {
				array_splice($region, $i, 1);
				$i--;
			}
		}

		if (count($category) > 0 && count($region) > 0 && $member["u_category"] != "" && $member["u_region"] != "") {

			$category = $category[0];
			$region = $region[0];
			$category_name = array_search($category, $EXTERNAL_CONSTANTS["categoryforpage"]);

			$short_url = "site/" . $category_name . ".php?region=r" . $region . "&sponsor=" . $member["u_id"];

			$description = $member["u_desc"] != "" ? $member["u_desc"] : "No description provided.";
			$description = strlen($description) > 150 ? substr($description, 0, 150) . "..." : $description;

			addContent("<li><h4><a href=\"../" . $short_url . "&search=" . implode(" ", $SEARCH_TERMS) . "\">" . $member["u_login_name"] . "</a></h4>");
			addContent("<p>Desc: " . $description . "</p>");
			addContent("<p class=\"url\">URL: <a href=\"../" . $short_url . "&search=" . implode(" ", $SEARCH_TERMS) . "\">" . $short_url . "</a></p>");
			addContent("</li>");
		}

	}
	addContent("</ul>");

	# Pages
	addContent("<a name=\"pages\"></a><h3>Pages: (go to <a href=\"#members\">members</a>)</h3>");
	addContent("<ul class=\"sponsorlist\">");
	foreach ($pages_record as $rc => $record) {

		# Path
		$path = substr($_SERVER["REQUEST_URI"], 0, strrpos($_SERVER["REQUEST_URI"], "/"));
		$path = substr($path, 0, strrpos($path, "/"));
		
		# URL
		$url = "http://" . $_SERVER["SERVER_NAME"] . $path . "/" . $record["p_section"] . "/" . $record["p_id"] . ".php";

		# Short URL
		$short_url = $record["p_section"] . "/" . $record["p_id"] . ".php";

		# Get short version of content
		$short_content = $record["p_content"];

		# How much on each side of the found word will be shown?
		$word_spread = 98;

		# First occurance of first search item
		$short_content_first_index = strpos(strtolower($short_content), strtolower($SEARCH_TERMS[0]));
		$start_pos = ($short_content_first_index > $word_spread) ? $short_content_first_index - $word_spread : 0;
		$end_pos = ($short_content_first_index < strlen($short_content) - $word_spread) ? $short_content_first_index + $word_spread : strlen($short_content);

		# Cut
		$short_content = substr($short_content, $start_pos, $end_pos - $start_pos);

		# Content
		addContent("<li><h4><a href=\"../" . $short_url . "\">" . $record["p_name"] . "</a></h4>");
		addContent("<p>" . $short_content . "</p>");
		addContent("<p class=\"url\">URL: <a href=\"../" . $short_url . "\">" . $short_url . "</a></p>");
		addContent("</li>");
	}
	addContent("</ul>");

} else {

	addContent("<h2>Search terms missing</h2>");
	addContent("<p>No search terms were entered.</p>");
}



?>